package com.dongjiantong.web.boss.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.biz.model.FarmStock;
import com.dongjiantong.biz.service.FarmStockService;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.dongjiantong.sys.service.AnimalTypeService;
import com.dongjiantong.user.enums.FarmsEnum;
import com.dongjiantong.user.model.Farms;
import com.dongjiantong.user.model.dtos.FarmStockDto;
import com.dongjiantong.user.service.FarmsService;
import com.dongjiantong.user.service.MzUserService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查看养殖场的信息
 *
 * @author cao
 * @create 2017-11-15 上午11:27
 **/
//@RestController
//@RequestMapping("/api/boss/farms")
@Slf4j
@OpenBean

public class FarmsController {

    @Reference(version = "1.0.0")
    public FarmsService farmsService;

    @Reference(version = "1.0.0")
    public MzUserService mzUserReadService;

    @Reference(version = "1.0.3")
    public AnimalTypeService animalTypeService;
    @Reference(version = "1.0.0")
    public FarmStockService farmStockService;

    /**
     * 查看所有养殖场列表
     * 区域（数组）
     *
     * @param farmId     养殖场名字
     * @param linkman    联系人名字
     * @param phone      手机号
     * @param animalType 动物种类（数组）
     * @param pageNum    页码
     * @param pageSize   一页数
     * @return
     */
    @OpenMethod(
            key = "boss.farms.breedList",
            paramNames = {"areaList", "farmId", "linkman", "phone", "animalType", "pageNum", "pageSize"},
            httpMethods = RequestMethod.GET
    )
    public Response<PageInfo<Farms>> farmsBreedList(List<Integer> areaList,
                                                    Long farmId,
                                                    String linkman,
                                                    String phone,
                                                    List<Integer> animalType,
                                                    Integer pageNum,
                                                    Integer pageSize) {

        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }


        //基本搜索
        Farms farms = new Farms();
        farms.setFarmsType(FarmsEnum.Breed.getCode());
        Integer areaSize = areaList.size();
        switch (areaSize) {
            case 1:
                farms.setProvinceId(areaList.get(0));
                break;
            case 2:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                break;
            case 3:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                break;
            case 4:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                farms.setStreetId(areaList.get(3));
                break;
            case 5:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                farms.setStreetId(areaList.get(3));
                farms.setVillageId(areaList.get(4));
                break;
            default:
                break;


        }

        farms.setId(farmId);
        farms.setLinkman(linkman);
        farms.setPhone(phone);

        if (animalType != null) {
            Integer animalTypeLever = animalType.size();
            switch (animalTypeLever) {
                case 1:
                    farms.setAnimalOneType(animalType.get(0));
                    break;
                case 2:
                    farms.setAnimalTwoType(animalType.get(1));
                    break;
                case 3:
                    farms.setAnimalTwoType(animalType.get(1));
                default:
                    break;

            }
        }

        farms.setDeleteFlag(0); //boss可以看到所有

        PageInfo<Farms> pageInfo = farmsService.getFarmsListByPage(pageNum, pageSize, farms);


        return Response.ok(pageInfo);
    }

    /**
     * 养殖散户列表
     *
     * @param areaList
     * @param farmId
     * @param linkman
     * @param phone
     * @param animalType
     * @param pageNum
     * @param pageSize
     * @return
     */
    @OpenMethod(key = "boss.farms.farmsRetailList",
            paramNames = {"areaList", "farmId", "linkman", "phone", "animalType", "pageNum", "pageSize"},
            httpMethods = RequestMethod.GET)
    public Response<PageInfo<Farms>> farmsRetailList(List<Integer> areaList,
                                                     Long farmId,
                                                     String linkman,
                                                     String phone,
                                                     List<Integer> animalType,
                                                     Integer pageNum,
                                                     Integer pageSize) {

        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }


        //基本搜索
        Farms farms = new Farms();
        farms.setFarmsType(FarmsEnum.Retail.getCode());

        Integer areaSize = areaList.size();
        switch (areaSize) {
            case 1:
                farms.setProvinceId(areaList.get(0));
                break;
            case 2:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                break;
            case 3:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                break;
            case 4:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                farms.setStreetId(areaList.get(3));
                break;
            case 5:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                farms.setStreetId(areaList.get(3));
                farms.setVillageId(areaList.get(4));
                break;
            default:
                break;


        }
        farms.setId(farmId);
        farms.setLinkman(linkman);
        farms.setPhone(phone);
        if (animalType != null) {
            Integer animalTypeLever = animalType.size();
            switch (animalTypeLever) {
                case 1:
                    farms.setAnimalOneType(animalType.get(0));
                    break;
                case 2:
                    farms.setAnimalTwoType(animalType.get(1));
                    break;
                case 3:
                    farms.setAnimalTwoType(animalType.get(1));
                default:
                    break;

            }
        }
        farms.setDeleteFlag(0); //boss可以看到所有
        PageInfo<Farms> pageInfo = farmsService.getFarmsListByPage(pageNum, pageSize, farms);
        return Response.ok(pageInfo);
    }

    /**
     * 屠宰场列表
     *
     * @param areaList
     * @param farmId
     * @param linkman
     * @param phone
     * @param pageNum
     * @param pageSize
     * @return
     */

    //@GetMapping("/farmsButcherList")
    @OpenMethod(
            key = "boss.farms.butcherList",
            paramNames = {"areaList", "farmId", "linkman", "phone", "animalType", "pageNum", "pageSize"},
            httpMethods = RequestMethod.GET
    )
    public Response<PageInfo<Farms>> farmsButcherList(List<Integer> areaList,
                                                      Long farmId,
                                                      String linkman,
                                                      String phone,
                                                      List<Integer> animalType,
                                                      Integer pageNum,
                                                      Integer pageSize) {

        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }


        //基本搜索
        Farms farms = new Farms();
        farms.setFarmsType(FarmsEnum.Butcher.getCode());

        Integer areaSize = areaList.size();
        switch (areaSize) {
            case 1:
                farms.setProvinceId(areaList.get(0));
                break;
            case 2:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                break;
            case 3:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                break;
            case 4:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                farms.setStreetId(areaList.get(3));
                break;
            case 5:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                farms.setStreetId(areaList.get(3));
                farms.setVillageId(areaList.get(4));
                break;
            default:
                break;


        }
        farms.setId(farmId);
        farms.setLinkman(linkman);
        farms.setPhone(phone);

        if (animalType != null) {
            Integer animalTypeLever = animalType.size();
            switch (animalTypeLever) {
                case 1:
                    farms.setAnimalOneType(animalType.get(0));
                    break;
                case 2:
                    farms.setAnimalTwoType(animalType.get(1));
                    break;
                case 3:
                    farms.setAnimalTwoType(animalType.get(1));
                default:
                    break;

            }
        }
        farms.setDeleteFlag(0); //boss可以看到所有


        PageInfo<Farms> pageInfo = farmsService.getFarmsListByPage(pageNum, pageSize, farms);


        return Response.ok(pageInfo);
    }

    /**
     * 根据ID查询详情养殖场/散户/屠宰场
     *
     * @param farmId
     * @return
     */
    @OpenMethod(key = "boss.farms.selectByFarmId",
            paramNames = {"farmId"},
            httpMethods = RequestMethod.GET)
    public Response<FarmStockDto> selectByFarmId(Long farmId) {
        try {
            FarmStockDto farmsDto = new FarmStockDto();
            Farms farms = farmsService.selectByPrimaryKey(farmId);
            farmsDto.setFarms(farms);

            if (farms.getFarmsType() == FarmsEnum.Breed.getCode() || farms.getFarmsType() == FarmsEnum.Retail.getCode()) {
                FarmStock farmStock = farmStockService.selectByFarmID(farmId);
                farmsDto.setFarmStock(farmStock);
            }
            return Response.ok(farmsDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new OPServerException(HttpCode.INTERNAL_SERVER_ERROR, "请稍后再试!");
        }


    }

    /**
     * 搜索菜单根据区域查询Farm
     *
     * @param areaList
     * @param farmType
     * @return
     */
    @OpenMethod(
            key = "boss.farms.farmListByArea",
            paramNames = {"areaList", "farmType"},
            httpMethods = RequestMethod.GET
    )
    public Response<List<Map>> farmsBreedList(List<Integer> areaList, Integer farmType
    ) {

        //基本搜索
        Farms farms = new Farms();
        farms.setFarmsType(farmType);
        Integer areaSize = areaList.size();
        switch (areaSize) {
            case 1:
                farms.setProvinceId(areaList.get(0));
                break;
            case 2:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                break;
            case 3:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                break;
            case 4:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                farms.setStreetId(areaList.get(3));
                break;
            case 5:
                farms.setProvinceId(areaList.get(0));
                farms.setCityId(areaList.get(1));
                farms.setRegionId(areaList.get(2));
                farms.setStreetId(areaList.get(3));
                farms.setVillageId(areaList.get(4));
                break;
            default:
                break;


        }


        //  farms.setDeleteFlag(1); //boss可以看到所有

        List<Farms> pageInfo = farmsService.select(farms);
        List<Map> mapList = new ArrayList<>();
        for (Farms farms1 : pageInfo) {
            Map map = new HashMap();
            map.put("value", farms1.getId());
            map.put("label", farms1.getFarmsname());
            mapList.add(map);
        }


        return Response.ok(mapList);
    }

    /**
     * Farm通用启用
     *
     * @param farmId
     * @param status
     * @return
     */
    @OpenMethod(
            key = "boss.farms.updateFarm",
            paramNames = {"farmId", "status"},
            httpMethods = RequestMethod.POST
    )
    public Response<String> updateBreedFarm(Long farmId, Integer status) {


        Farms farms = new Farms();

        farms.setId(farmId);
        farms.setStatus(status);
        try {

            int result = farmsService.updateByPrimaryKeySelective(farms);

            if (status == 1) {

                return Response.ok("启用成功");
            } else {
                return Response.ok("停用成功");
            }

        } catch (Exception e) {
            log.error(e.getMessage());
            if (status == 1) {

                throw new OPServerException(HttpCode.DATA_UPDATE_FAIL, "启用失败请稍后再试");
            } else {
                throw new OPServerException(HttpCode.DATA_UPDATE_FAIL, "停用失败请稍后再试");
            }

        }

    }

    /**
     * 下拉查询时，根据养殖类型取值
     *
     * @param farmType
     * @return
     */

    //@GetMapping("/querySelectFarmList")
    @OpenMethod(
            key = "boss.farms.selectFarmList",
            paramNames = {"farmType"},
            httpMethods = RequestMethod.GET
    )
    public Response<List<Farms>> querySelectFarmList(Integer farmType) {
        if (farmType == null || farmType == 0) {
            return Response.fail(HttpCode.BAD_REQUEST, "企业类型不能为空");
        }
        return Response.ok(farmsService.querySelectFarmList(farmType, ""));
    }


}
